package com.submit.demo.controller;

import com.submit.demo.annotation.AuthIgnore;
import com.submit.demo.common.api.CommonPage;
import com.submit.demo.common.api.CommonResult;
import com.submit.demo.dto.SubTagListParam;
import com.submit.demo.dto.SubmitterInsertParam;
import com.submit.demo.dto.SubmitterTaskData;
import com.submit.demo.model.Submitter;
import com.submit.demo.service.SubTaskService;
import com.submit.demo.service.SubmitterService;
import com.submit.demo.service.TaskService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("sub")
public class SubmitterController {
    @Autowired
    private TaskService taskService;

    @Autowired
    private SubTaskService subTaskService;

    @Autowired
    private SubmitterService submitterService;

    @AuthIgnore
    @ApiOperation("提交人查看任务详情")
    @ResponseBody
    @RequestMapping(value = "/lok", method = RequestMethod.GET)
    public CommonResult<SubmitterTaskData> lokTask(@RequestParam("sid") String sid, @RequestParam("tid") String tid){
        SubmitterTaskData subBriefTask = taskService.selectSubTaskByTid(sid, tid);
        return CommonResult.success(subBriefTask, "查询成功");
    }

    @AuthIgnore
    @ApiOperation("上传文档")
    @ResponseBody
    @PostMapping("upload")
    public CommonResult subTask(@RequestParam("sid") String sid, @RequestParam("tid") String tid, @RequestParam("words")MultipartFile[] files){
        int result = subTaskService.upload(sid, tid, files);
        if(result > 0) return CommonResult.success(null, "上传成功");
        return CommonResult.failed("上传失败");
    }

    @AuthIgnore
    @ApiOperation("新建提交人")
    @ResponseBody
    @RequestMapping(value = "/crsub", method = RequestMethod.GET)
    public CommonResult crSub(SubmitterInsertParam subInsertParam){
        int result = submitterService.crSub(subInsertParam);
        if(result > 0) return CommonResult.success(null, "创建成功");
        return CommonResult.failed("创建失败");
    }

    @ApiOperation("批量导入提交人")
    @ResponseBody
    @RequestMapping(value = "/impSubs", method = RequestMethod.POST)
    public CommonResult impSubs(@RequestParam("cid") String cid, @RequestParam("file")MultipartFile file){
        int result = submitterService.insertFromCsv(cid, file);
        if(result > 0) return CommonResult.success(null, "创建成功");
        return CommonResult.failed("创建失败");
    }

    @ApiOperation("提交人标签列表")
    @ResponseBody
    @RequestMapping(value = "/subTagList", method = RequestMethod.GET)
    public CommonResult<CommonPage<Submitter>> getList(SubTagListParam param, @RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "10") Integer pageSize){
        CommonPage<Submitter> list = submitterService.getListWithTag(param.getCid(), param.getTags(), currentPage, pageSize);
        return CommonResult.success(list);
    }

    @ApiOperation("获取所有提交人")
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<Submitter>> getSubmitters(@RequestParam(defaultValue = "25") Integer pageSize, @RequestParam(defaultValue = "1") Integer pageNum){
        CommonPage<Submitter> list = submitterService.getList(pageNum, pageSize);
        return CommonResult.success(list);
    }

    @ApiOperation("删除提交人")
    @ResponseBody
    @RequestMapping(value = "/del", method = RequestMethod.GET)
    public CommonResult delSub(String sid){
        Integer result = submitterService.del(sid);
        if(result > 0) return CommonResult.success(null, "删除成功");
        return CommonResult.failed("删除失败");
    }
}
